package com.example.xyh.serviceImpl;

import com.example.xyh.dao.ILineItemDao;
import com.example.xyh.dao.IShopDao;
import com.example.xyh.dao.IShopItemDao;
import com.example.xyh.entity.LineItem;
import com.example.xyh.entity.Shop;
import com.example.xyh.entity.ShopItem;
import com.example.xyh.service.ShopItemService;
import jakarta.annotation.Resource;
import jakarta.transaction.Transactional;
import lombok.extern.apachecommons.CommonsLog;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
@CommonsLog
public class ShopItemServiceImpl implements ShopItemService {
    @Resource
    IShopDao iShopDao;
    @Resource
    ILineItemDao iLineItemDao;
    @Resource
    IShopItemDao iShopItemDao;
    @Autowired
    protected ModelMapper modelMapper;

    @Override
    public com.example.xyh.dto.ShopItemDto addShopItem(Integer shop_id, Integer item_id) {
        Shop shop = iShopDao.findByShopId(shop_id);
        LineItem lineItem = iLineItemDao.findByItemId(item_id);
        ShopItem newShopItem = ShopItem.builder()
                .shop(shop)
                .lineItem(lineItem)
                .build();
        iShopItemDao.save(newShopItem);
        return modelMapper.map(newShopItem, com.example.xyh.dto.ShopItemDto.class);
    }
    @Transactional
    @Override
    public Integer deleteShopItem(Integer shop_id, Integer item_id) {
        return iShopItemDao.deleteByShopIdAndItemId(shop_id, item_id);
    }

    @Override
    public List<com.example.xyh.dto.ShopItemDto> shopItemList(Integer shop_id) {
        List<ShopItem> shopItemList = iShopItemDao.findByShopShopId(shop_id);
        List<com.example.xyh.dto.ShopItemDto> orderListDTOList = new ArrayList<>();

        for(ShopItem shopItem : shopItemList){
            com.example.xyh.dto.ShopItemDto shopItemDTO = modelMapper.map(shopItem, com.example.xyh.dto.ShopItemDto.class);
            orderListDTOList.add(shopItemDTO);
        }

        return orderListDTOList;
    }
}
